package com.mononite.ac

import spock.lang.Specification

class AhoCorasickTest extends Specification {
	def 'throw exception when construct with invalid words'() {
		when:
		new AhoCorasick(words)

		then:
		thrown(IllegalArgumentException)

		where:
		words << [null, [], ['']]
	}
	
	def 'no word match'() {
		setup:
		def instance = new AhoCorasick(['forNoMatch'])

		expect:
		instance.matches(text).empty
	
		where:
		text << [null, '', 'hello, world!']
	}

	def 'simple match'() {
		setup:
		def instance = new AhoCorasick(['hello', 'world'])
		
		when:
		def results = instance.matches('hello, world!')

		then:
		results == [[start : 0, word : 'hello'], [start : 7, word : 'world']] 
	}

	def 'match example from wikipedia'() {
		setup:
		def instance = new AhoCorasick(['a', 'ab', 'bc', 'bca', 'c', 'caa'])
		
		when:
		def results = instance.matches('abccab')

		then:
		results.size() == 7
		results[0] == [start : 0, word : 'a']
		results[1] == [start : 0, word : 'ab']
		results[2] == [start : 1, word : 'bc']
		results[3] == [start : 2, word : 'c']
		results[4] == [start : 3, word : 'c']
		results[5] == [start : 4, word : 'a']
		results[6] == [start : 4, word : 'ab']
	}
}

